package code.part01_base;

import org.junit.jupiter.api.Test;


/**
 * @author: ZJT
 * @Time: 2021/7/2
 * @Description: 数组
 */
public class P07_Array {

    @Test
    public void arrayInit() {
        //声明数组
        int[] array;
        //创建内存空间
        array = new int[5];
        //数组赋值
        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        array[3] = 3;
        array[4] = 4;
        //数组操作
        System.out.println(array[0]);
    }

    @Test
    public void arrayInitWay() {
        //数组的几种创建方式
        //1、声明并申请空间
        int[] arr1 = new int[5];
        int[] arr2 = new int[5];
        int arr3[] = new int[5];
        //2、声明数组并赋值
        int[] arr4 = new int[]{1, 2, 3, 4, 5};
        //3、直接初始化操作
        int[] arr5 = {1, 2, 3};

        //获取数组的长度
        System.out.println(arr1.length);
    }

    @Test
    public void arrayElementDefault() {
		/*
		数组是引用类型，当创建完成数组之后相当于是在方法外定义了一个变量，此时数组中的值是有默认值的
			默认是什么，取决于你定义的数组的类型：
			int:0
			String null
			boolean false
		*/
        int[] array = new int[5];
        System.out.println(array[0]);
    }

    @Test
    public void arrayTwoDimension() {
        int[] arr = new int[6];
        int[][] arr2 = new int[3][];
        //创建二维数组的对象
        arr2[0] = new int[5];
        arr2[1] = new int[3];
        arr2[2] = new int[4];
        //赋值
        arr2[0][0] = 1;
        arr2[0][1] = 2;
        arr2[0][2] = 3;
        arr2[0][3] = 4;
        arr2[0][4] = 5;

        arr2[1][0] = 6;
        arr2[1][1] = 7;
        arr2[1][2] = 8;

        arr2[2][0] = 9;
        arr2[2][1] = 10;
        arr2[2][2] = 11;
        arr2[2][3] = 12;

        for(int i = 0;i<arr2.length;i++){
            for(int j = 0;j<arr2[i].length;j++){
                System.out.print(arr2[i][j]+"\t");
            }
            System.out.println();
        }
    }

}
